package cn.bluemobi.platform.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.member.Merchant;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.mapper.CommodityMapper;
import cn.bluemobi.platform.mapper.MerchantMapper;
import cn.bluemobi.platform.service.MerchantService;

@Service
@Transactional
public class MerchantServiceImpl implements MerchantService {

	@Autowired
	private MerchantMapper merchantMapper;

	@Autowired
	private CommodityMapper commodityMapper;

	@Override
	public Page<Map<String, Object>> findMerchants(PageCondition cond, User user) {
		Page<Map<String, Object>> page = PageUtils.startPage(cond);
		cond.getMap().put("merchantId", user.getMerchantId());
		page.setList(merchantMapper.findMerchantPages(cond.getMap()));
		return page;
	}

	@Override
	public Merchant findMerchantDetail(String merchantId) {
		Merchant merchant = merchantMapper.findMerchantDetail(merchantId);
		return merchant;
	}

	@Override
	public MapDto saveOrUpdate(Merchant merchant, User user) {
		MapDto dto = new MapDto();
		if (StringUtils.isEmpty(merchant.getMerchantId())) {
			// 新增
			merchantMapper.insertMerchant(merchant);
		} else {
			// 编辑
			merchantMapper.updateMerchant(merchant);
		}
		return dto;
	}

	@Override
	public MapDto deleteMerchant(String merchantId, User user) {
		MapDto dto = new MapDto();
		if (merchantMapper.checkIsExistCommodityByMerchantId(merchantId) > 0) {
			dto.errorMsg("删除失败，该商户下存在商品");
			return dto;
		}
		// commodityMapper.deleteCommodityByMerchantId(merchantId);
		merchantMapper.deleteMerchant(merchantId);
		return dto;
	}
}
